import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:untitled/core/store/user_store.dart';
import 'package:untitled/core/utils/navigate_to.dart';

import '../../../core/network/request.dart';
import '../../../core/utils/toast.dart';
import '../../../model/user_info/user_info_model.dart';
import '../../../pages/home/friend_info/friend_info.dart';

class AddFriendViewModel extends ChangeNotifier {
  final _request = RequestService();

  Future<void> search(String value) async {
    final response = await _request.get('/friend/search', queryParameters: {"im_id": value});
    final data = response.data;
    final friendInfo = UserInfo(username: data.username, sex: data.sex, avatar: data.avatar, imId: data.im_id);
    final loginData = jsonDecode(Store().getString('login_data'));
    final String imId = loginData['imId'];
    if(imId == friendInfo.imId) {
      toast('不能添加自己为好友');
      return;
    }
    navigateTo(FriendInfoPage(friendInfo: friendInfo,));
  }
}